<template>
  <Spin :spinning="loading" tip="Loading...">
    <div>
      <!-- 在库车质量信息维护-->
      <TopFilter
        :list="topFilterList"
        :cols="3"
        @filterChange="changeHandle"
        @onCollapse="collapseHandle"
        :labelWidth="110"
      ></TopFilter>

      <FilterTable
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :is-select-column="false"
        :onRowSelectChange="selectChangeHandle"
        :onColumnsChange="columns => this.columns = columns"
      >
        <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" @click="addHandle()">导 出</multiuse-button>
        </template>
      </FilterTable>


      <BaseDialog :visible.sync="visibleAdd" :title="'选择经销商'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
        <DealerList @close="closeAdd" @save="saveAdd"></DealerList>
      </BaseDialog>
      <BaseDialog :visible.sync="visible" :title="'车型新增'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
        <vehicle @close="close" @save="save"></vehicle>
      </BaseDialog>
      <Drawer
        :visible.sync="detailVisible"
        :title="'在库车质量信息明细'"
        destroyOnClose
        :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
        width="72%"
      >
        <detailModel :rowData="formData" @close="emitcloseHandler"/>
      </Drawer>

      <Drawer
        :visible.sync="editVisible"
        :title="'在库车质量信息编辑'"
        destroyOnClose
        :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
        width="72%"
      >
        <editModel :rowData="formData" @close="editcloseHandler"/>
      </Drawer>
    </div>
  </Spin>
</template>
<script>
import {getListByPage, exportList,exporturl } from '@/api/claim/dealer/vehicleQuality/index.js'
import detailModel from './detailModel'
import editModel from './editModel'
import DealerList from '@/pages/claimDemage/tools/DealerList.vue';
import vehicle from '@/pages/claimDemage/tools/ModelVehicle.vue';
import { exportExcelFile } from '@/utils';
import { getPartType } from '@/api/dictionary/index';
import { dictionary } from '@/utils/dictMixin';
export default {
  name:'acknowledgementOrder',
  data(){
    this.BaseTable=null;
    return{
      topFilterList:this.createdFrom(),
      columns:this.createdTable(),
      fetchParams:{

      },
      fetchApi:getListByPage,
      formData:{},
      detailVisible:false,
      editVisible:false,
      dealerCode:'',
      ProjectId:'',
      selectedRows: [],
      visibleAdd: false,
      loading: false,
      visible:false
    }
  },
  components: {
    vehicle,
    DealerList,
    detailModel,
    editModel
  },
  mixins:[dictionary],
  mounted(){
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created(){
     this.init();
  },
  methods:{
    createdFrom(){
      return [
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
        },
        {
          type: 'SELECT',
          label: '处理状态',
          fieldName: 'dealStatus',
          placeholder: '请选择...',
          initialValue: '',
          itemList: [],
          filterable: true
        },
        {
          type: 'SELECT',
          label: '电池状态',
          fieldName: 'batteryStatus',
          placeholder: '请选择...',
          initialValue: '',
           itemList: [],
          filterable: true
        },
        {
          type: 'SELECT',
          label: '使用状态',
          fieldName: 'useStatus',
          placeholder: '请选择...',
          initialValue: '',
           itemList: [],
          filterable: true
        },
        // {
        //   type: 'INPUT',
        //   label: '选择经销商',
        //   fieldName: 'ownerCode',
        //   placeholder: '请输入',
        //   initialValue: '',
        //   unitRender: p => {
        //     return (
        //       <div>
        //         <multiuse-button icon="el-icon-search" onClick={() => this.chooseDealerNo()} />
        //       </div>
        //     );
        //   }
        // },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'modelCode',
          placeholder: '请输入',
          initialValue: '',
          unitRender: (p) => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.chooseOrderNo()} />
              </div>
            );
          },
        },
        {
          type: 'RANGE_DATE',
          label: '检测日期',
          fieldName: 'beginDetectDate|endDetectDate',
          placeholder: '请选择',
          initialValue:[],
          dateType:'exactdaterange'
        },

      ]
    },
    createdTable(){
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 90,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>

                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 2)}>
                  明细
                </multiuse-button>
                <multiuse-button size="mini" type="text" onClick={() => this.detailHandle(props.row, 2)}>
                  编辑
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '使用状态',
          dataIndex: 'useStatusStr',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '电池状态',
          dataIndex: 'batteryStatusStr',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '最近一次处理状态',
          dataIndex: 'dealStatusStr',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '生产日期',
          dataIndex: 'prodDate',
          sorter: true,
          filter: true,
          filterType: 'input',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '入库日期',
          dataIndex: 'stockDate',
          sorter: true,
          filter: true,
          filterType: 'input',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '在库时长（月）',
          dataIndex: 'monthNum',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '连续异常次数',
          dataIndex: 'num',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '最近处理人',
          dataIndex: 'dealBy',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '处理时间',
          dataIndex: 'dealAt',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
      ]
    },
    selectChangeHandle(row){
      this.selectedRows = row;
    },
    chooseDealerNo() {
      this.visibleAdd = true;
    },
   async addHandle() {
      let json = Object.assign({}, { CODE: 'CODE_00000224' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     let json = this.fetchParams;
      //     exportList(json)
      //       .then(response => {
      //         console.log(response, 'response');
      //         exportExcelFile(response, '在库车质量信息');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },
    async init(){
      const res1 = await getPartType( {type:5560});
      if (res1.resultCode == 200) {
        this.topFilterList.find(x => x.fieldName == 'batteryStatus').itemList = res1.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      }
      const res2 = await getPartType( {type:5596});
      if (res2.resultCode == 200) {
        this.topFilterList.find(x => x.fieldName == 'dealStatus').itemList = res2.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      }
      const res3 = await getPartType( {type:5571});
      if (res3.resultCode == 200) {
        this.topFilterList.find(x => x.fieldName == 'useStatus').itemList = res3.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      }
    },
    changeHandle(params){
      this.fetchParams.dealerCode=this.dealerCode;
      this.fetchParams.projectCode=this.ProjectId;
      this.fetchParams=Object.assign({},this.fetchParams,params)
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data){
      this.formData=data;
      this.detailVisible=true
    },
    detailHandle(data){
      this.formData=data;
      this.editVisible=true
    },
    emitcloseHandler(type){
      if(type){
        this.detailVisible=false;
      }else {
        this.detailVisible=false;
        this.fetchParams = { ...this.fetchParams };
      }
    },
    editcloseHandler(type){
      if(type){
        this.editVisible=false;
      }else {
        this.editVisible=false;
        this.fetchParams = { ...this.fetchParams };
      }
    },
    closeAdd() {
      this.visibleAdd = false;
    },
    saveAdd(val) {
      this.topFilterList.find(x => x.fieldName == 'ownerCode').initialValue = val[0].dealerCode;
      this.visibleAdd = false;
    },
    save(obj) {
      this.topFilterList.find((x) => x.fieldName == 'modelCode').initialValue = obj[0].modelCode;
      this.visible = false;
    },
    close() {
      this.visible = false;
    },
    chooseOrderNo() {
      this.visible = true;
    },
  }
}
</script>
